package com.univhis.exception;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.univhis.common.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2024/1/4 9:50
 * 全局自定义异常处理
 */
@ControllerAdvice(basePackages = "com.univhis.controller")
public class GlobalExceptionHandler {

    private static final Log log = LogFactory.get();

    /**
     * 统一异常处理@ExceptionHander, 主要用于Exception
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody // 返回 json 串
    public Result<?> error(HttpServletRequest request, Exception e) {
        log.error("异常信息",  e);
        return Result.error("-1", "系统异常");
    }

    @ExceptionHandler(CustomException.class)
    @ResponseBody
    public Result<?> customError(HttpServletRequest request, CustomException e) {
        return Result.error(e.getCode(), e.getMsg());
    }
}
